package com.wustl.android.hood;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewInflate;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;

public class MainToolbar extends RelativeLayout {
	
	boolean mToolbarExpanded = false;
	AdapterView.OnItemClickListener clickListener = null;
	ImageAdapter mAdapter = null;
	
	public MainToolbar(Context context, AttributeSet attrs, Map inflateParams) {
		super(context, attrs, inflateParams);
		// TODO Auto-generated constructor stub
		ViewInflate vi = (ViewInflate)
		context.getSystemService(Context.INFLATE_SERVICE);

		vi.inflate(R.layout.maintoolbar_buttons, this, true, inflateParams);
		vi.inflate(R.layout.maintoolbar,this, true, inflateParams);
		
		// Create "Expand & Shrink" button listener
		ImageButton btnExpand = (ImageButton) findViewById(R.id.expand_menu);
		btnExpand.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				ImageButton btnExpand = (ImageButton) findViewById(R.id.expand_menu);

				View layout = findViewById(R.id.MainToolbar_Buttons);
				
				if( mToolbarExpanded == true ) {
					btnExpand.setImageResource(R.drawable.expand);
					layout.setVisibility(View.INVISIBLE);
				}
				else {
					btnExpand.setImageResource(R.drawable.shrink);
					layout.setVisibility(View.VISIBLE);
				}
				
				mToolbarExpanded = !mToolbarExpanded;
            } });
		
		GridView g = (GridView)findViewById(R.id.MainToolbar_Buttons);
		mAdapter = new ImageAdapter(context);
		g.setAdapter(mAdapter);
		
		clickListener = new AdapterView.OnItemClickListener() {
		 	public void   onItemClick(AdapterView parent, View v, int position, long id) {
		 		onClick(v, position, id);
		 	}
		}; 

		g.setOnItemClickListener(clickListener);
	}
	
	public class ImageAdapter extends BaseAdapter {
		
		public final Integer[] mThumbIds = {
				R.drawable.hood,
				R.drawable.facebook,
				R.drawable.images,
				R.drawable.map,
		};
		public final Class[] mThumbClass = {
				HoodMain.class,
				FaceBookMain.class,
				FaceBookMain.class,
				MapMain.class,
		};
		
		public ImageAdapter(Context c) {
			mContext = c;
		}
		
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ImageView i = new ImageView(mContext);

            i.setImageResource(mThumbIds[position]);
            i.setLayoutParams(new Gallery.LayoutParams(50, 50));
            i.setAdjustViewBounds(false);
            i.setScaleType(ImageView.ScaleType.CENTER_CROP);
            i.setPadding(8, 8, 8, 8);

            return i;
		}

		public int getCount() { return mThumbIds.length; }
		public Object getItem(int position) { return position; }
		public long getItemId(int position) { return position; }
 
		
		private Context mContext;
	}
	
	
	protected void onClick(View v, int position, long id) {
		Intent i = new Intent(mContext, mAdapter.mThumbClass[position]);
		((Activity) mContext).startSubActivity(i, 1);
	}
	
	protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }
}
